package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Chatting;
import org.csu.secondhand.api.persistence.ChattingMapper;
import org.csu.secondhand.api.service.ChattingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("chattingService")
public class ChattingServiceImpl implements ChattingService {
    @Autowired
    ChattingMapper chattingMapper;

    @Override
    public Chatting insertChatting(Chatting chatting) {
        chattingMapper.insert(chatting);
        return chatting;
    }

    @Override
    public List<Chatting> getChattingByUser(String user) {
        QueryWrapper<Chatting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user1", user).or().eq("user2", user);
        queryWrapper.orderByDesc("time");
        List<Chatting> chattingList = chattingMapper.selectList(queryWrapper);
        for (int i = 0; i < chattingList.size(); i++) {
            if (chattingList.get(i).getContent().equals(""))
                chattingList.remove(i);
        }
        return chattingList;
    }

    @Override
    public Chatting sendMsg(String user1, String user2, String content) {
        Chatting chatting = getChatting(user1, user2);
        if(chatting == null) {
            chatting = new Chatting(0, user1, user2, "", "", "", 0);
            insertChatting(chatting);
        }
        if(chatting.getContent() != "") {
            chatting.setTime(TimeUtil.getTime());
            chatting.setContent(chatting.getContent() + "`" + content);
            if (chatting.getUser1().equals(user1)) {
                chatting.setWho(chatting.getWho() + "`" + 0);
                chatting.setUnread(2);
            } else if (chatting.getUser2().equals(user1)) {
                chatting.setWho(chatting.getWho() + "`" + 1);
                chatting.setUnread(1);
            }
        }
        else {
            chatting.setTime(TimeUtil.getTime());
            chatting.setContent(content);
            if (chatting.getUser1().equals(user1)) {
                chatting.setWho("0");
                chatting.setUnread(2);
            } else if (chatting.getUser2().equals(user1)) {
                chatting.setWho("1");
                chatting.setUnread(1);
            }
        }
        chattingMapper.updateById(chatting);
        return chatting;
    }

    @Override
    public Chatting getChatting(String user1, String user2) {
        QueryWrapper<Chatting> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(qw -> qw.eq("user1", user1).eq("user2", user2))
                .or(qw -> qw.eq("user1", user2).eq("user2", user1));
        Chatting chatting = chattingMapper.selectOne(queryWrapper);
        return chatting;
    }

    @Override
    public Chatting iReadIt(long id) {
        Chatting chatting = chattingMapper.selectById(id);
        chatting.setUnread(0);
        chattingMapper.updateById(chatting);
        return chatting;
    }


}
